const app = getApp()
Page({
  data: {
    stations:app.globalData.stations || [],
    station:{},
    clickGas:false,
    markers: [],
    newsList:[],
    point:{
      long:  110.318012,
      lat: 20.012226
    },
    newPoint: {
      long: 110.318012,
      lat: 20.012226
    }
  },
  onLoad (){
    let _this = this;

    this.getGasStation();
    this.mapCtx = wx.createMapContext('J_oilMap')
    this.getNews();
  },
  regionchange(e) {
    let thisEle =this;
    this.mapCtx = wx.createMapContext('J_oilMap')
    if(e.type!=='end'){
      return false;
    }
    console.log(e.type)
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res)
        if (res.latitude != thisEle.data.newPoint.lat && res.longitude != thisEle.data.newPoint.long) {
          thisEle.setData({
            'newPoint.lat': res.latitude,
            'newPoint.long': res.longitude
          })
          //获取当前范围的油站
          thisEle.getGasStation()

        }
      }
    })
  },

  movePoint (){
    this.mapCtx.moveToLocation()
  },
  closeOilDialog(){
    this.setData({
      'clickGas': false
    })
  },

  //获取新闻
  getNews(){
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'getNewsList', data: {
        pageSize: 1
      },
    }).then(res => {
      this.setData({
        'newsList': res.result.data
      })
      console.log(res.result.data)
    })
  },

  navSetPrice(e){
    console.log(this.data.station)
    app.globalData.stationId = this.data.station.id
    wx.navigateTo({
      url: '/pages/oilchange/index?stationid=' + this.data.station._id
    })
  },

  navSetStation(e) {
    wx.navigateTo({
      //url: '/pages/tipoff/index'
      url: '/pages/user/user'
    })
  },

  navSetStationPage (){
    wx.navigateTo({
      url: '/pages/station/index?stationid=' + this.data.station._id
    })
  },

  navToStation (e){
    let _this = this;
    let _marker = this.data.station;
    console.log(_marker)
    wx.getLocation({//获取当前经纬度
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: _marker.point.latitude,//要去的纬度-地址
          longitude: _marker.point.longitude,//要去的经度-地址
          name: _marker.name,
          address: _marker.address
        })
      }
    })
  },
  showNews(event){
    let id=event.target.dataset.id
    wx.navigateTo({
      url: '/pages/news/details/details?id=' + id
    })
  },

  markertap(e) {

    this.setData({
      station: this.data.stations[e.markerId],
      clickGas: true
    })
    
  },
  controltap(e) {
    console.log(e.controlId)
  },
  getGasStation (){
    let _this = this;
    let lat = this.data.newPoint.lat;
    let long = this.data.newPoint.long;
    wx.cloud.init()
      wx.cloud.callFunction({
        name: 'getStations',
        data:{
          latitude: lat,
          longitude: long,
        }
      }).then(res => {
        console.log(res)
        _this.setData({
          stations: res.result.data
        });
        this.setStations()
      })
  },

  setStations(){
    let stations = [];
    for (let i = 0; i < this.data.stations.length; i++) {
      let _d = this.data.stations[i]
      let priceArr = '';
      if (_d.oil_price) {

        for (let k = 0; k < _d.oil_price.oils.length; k++) {
          let oil = _d.oil_price.oils[k]
          priceArr += oil.oil_type + ':' + '￥' + oil.price + '\n';
        }
      }

      stations.push(
        {
          iconPath: "/resources/gasstation.png",
          id: i,
          latitude: _d.point.coordinates[1],
          longitude: _d.point.coordinates[0],
          width: 24,
          height: 24,
          title: _d.name,
          callout: {
            content: _d.name, //+ '\n' + priceArr,
            padding: 3,
            color: '#ffffff',
            bgColor: _d.open ? '#ffca27' : '#dddddd',
            borderRadius: 3,
            display: 'ALWAYS'
          }
        }
      )
    }
    console.log(stations)

    this.setData({
      'markers': stations
    })
  }

})